﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dami;
public partial class admin_Modules_hotro_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
       // this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
        if (!this.IsPostBack) {
            InitItem();
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }

    private void AddNew()
    {
        String strMess = "";
        int flag = -1;
        try
        {
             int ID=-1;
       
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
       
            WebData data = new WebData();
            clsFileUpLoad files = new clsFileUpLoad();
            String pathImage = "";

            if (String.IsNullOrEmpty(FileUpload1.FileName))
            {
                pathImage = Session["ht_Image"] == null ? "" : Session["ht_Image"].ToString();
            }
            else {

                files.DeleteFile(Session["ht_Image"].ToString());
                pathImage = files.FileSaveImage("upload/image", this.FileUpload1);
            }
            Session.Remove("ht_Image");
            DataLibrary.tbl_Hotro item = new DataLibrary.tbl_Hotro();
            item.Id = ID;
            item.Image = pathImage;
            item.IsActive = 1;
            item.OrderId = Convert.ToInt32(txtorderId.Text);
            item.Title = txtTitle.Text;
            item.Url = txtUrl.Text;
            item.IdTable = cbbidCat.SelectedValue.ToString();
            item.IdTable = item.IdTable.Replace("0", "");
            item.Description = txtDescription.Text;
            data.tbl_Hotros.UpdateOnSubmit(item);
        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }
        if (flag > 0)
        {
            Response.Redirect(ViewState["URLReferrer"].ToString());
        }
        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);

    }

    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db=new WebData();
        DataLibrary.tbl_Hotro item = db.tbl_Hotros.GetItemById(ID);
        if (item != null) {
            txtDescription.Text = item.Description;
            txtorderId.Text = item.OrderId.ToString();
            txtTitle.Text = item.Title;
            txtUrl.Text = item.Url;
            Session["ht_Image"] = item.Image;
        }
    }
  
 
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}